package com.sizu.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sizu.common.dto.PurchaseListDto;
import com.sizu.common.dto.PurchaseListGoodsDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TPurchaseList;
import com.sizu.entity.TPurchaseListGoods;
import com.sizu.service.impl.TPurchaseListGoodsServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-21
 */
@RestController
@RequestMapping("/purchase/tPurchaseListGoods")
public class TPurchaseListGoodsController {

    @Autowired
    private TPurchaseListGoodsServiceImpl tPurchaseListGoodsServiceImpl;

    //做添加的方法
    @RequestMapping("/updatetPurchaseListGoods")
    public ResultEntity updatetPurchaseListGoods(PurchaseListGoodsDto purchaseListGoodsDto){
        TPurchaseListGoods tPurchaseListGoods = new TPurchaseListGoods();
        BeanUtils.copyProperties(purchaseListGoodsDto,tPurchaseListGoods);
        Wrapper<TPurchaseListGoods> wrapper = new EntityWrapper<TPurchaseListGoods>();
        wrapper.eq("code",purchaseListGoodsDto.getCode());
        TPurchaseListGoods tPurchaseListGoods1 = tPurchaseListGoodsServiceImpl.selectOne(wrapper);
       if(null!=tPurchaseListGoods1){
           tPurchaseListGoodsServiceImpl.updateById(tPurchaseListGoods);
       }
        return ResultEntity.ok();
    }

    //做删除的方法
    @RequestMapping("/deletetPurchaseListGoods")
    public ResultEntity deletetPurchaseListGoods(PurchaseListGoodsDto purchaseListGoodsDto){
        TPurchaseListGoods tPurchaseListGoods = new TPurchaseListGoods();
        BeanUtils.copyProperties(purchaseListGoodsDto,tPurchaseListGoods);
        Wrapper<TPurchaseListGoods> wrapper = new EntityWrapper<TPurchaseListGoods>();
        wrapper.eq("code",purchaseListGoodsDto.getCode());
        boolean delete = tPurchaseListGoodsServiceImpl.delete(wrapper);
        if(delete==true){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        return ResultEntity.ok();
    }

    //根据进货id查询出所有的商品
    @RequestMapping("/goodsList")
    public ResultEntity goodsList(Integer id){
        Wrapper<TPurchaseListGoods> wrapper = new EntityWrapper<TPurchaseListGoods>();
        wrapper.eq("purchase_list_id",id);
        List<TPurchaseListGoods> tPurchaseLists = tPurchaseListGoodsServiceImpl.selectList(wrapper);

        return ResultEntity.ok(tPurchaseLists);
    }
}
